package sys

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"github.com/vueadmin/utils/conv"
	"strings"
)

type Fields []string

func (fields Fields) Value() (driver.Value, error) {
	if len(fields) > 0 {
		return strings.Join(fields, ","), nil
	} else {
		return "", nil
	}
}

func (fields *Fields) Scan(value interface{}) error {
	str, ok := value.([]byte)
	if !ok {
		return errors.New("不匹配的数据类型")
	}
	var slice []string
	for _, v := range strings.Split(string(str), ",") {
		slice = append(slice, v)
	}
	*fields = slice
	return nil
}

type TabConfig []map[string]interface{}

func (tabConfig TabConfig) Value() (driver.Value, error) {
	if conv.String(tabConfig) == "[]" {
		return nil, nil
	}
	str, err := json.Marshal(tabConfig)
	if err != nil {
		return nil, err
	}
	if len(str) > 0 {
		return string(str), nil
	} else {
		return nil, nil
	}
}

func (tabConfig *TabConfig) Scan(value interface{}) error {
	str, ok := value.([]byte)
	if !ok {
		return errors.New("不匹配的数据类型")
	}
	json.Unmarshal(str, &tabConfig)
	return nil
}

type WithJoin []map[string]interface{}

func (withJoin WithJoin) Value() (driver.Value, error) {
	if conv.String(withJoin) == "[]" {
		return nil, nil
	}
	str, err := json.Marshal(withJoin)
	if err != nil {
		return nil, err
	}
	if len(str) > 0 {
		return string(str), nil
	} else {
		return nil, nil
	}
}

func (withJoin *WithJoin) Scan(value interface{}) error {
	str, ok := value.([]byte)
	if !ok {
		return errors.New("不匹配的数据类型")
	}
	json.Unmarshal(str, &withJoin)
	return nil
}

type ListFilter []map[string]interface{}

func (listFilter ListFilter) Value() (driver.Value, error) {
	if conv.String(listFilter) == "[]" {
		return nil, nil
	}
	str, err := json.Marshal(listFilter)
	if err != nil {
		return nil, err
	}
	if len(str) > 0 {
		return string(str), nil
	} else {
		return nil, nil
	}
}

func (listFilter *ListFilter) Scan(value interface{}) error {
	str, ok := value.([]byte)
	if !ok {
		return errors.New("不匹配的数据类型")
	}
	json.Unmarshal(str, &listFilter)
	return nil
}

type Other map[string]interface{}

func (other Other) Value() (driver.Value, error) {
	str, err := json.Marshal(other)
	if err != nil {
		return nil, err
	}
	if len(str) > 0 {
		return string(str), nil
	} else {
		return nil, nil
	}
}

func (other *Other) Scan(value interface{}) error {
	str, ok := value.([]byte)
	if !ok {
		return errors.New("不匹配的数据类型")
	}
	json.Unmarshal(str, other)
	return nil
}

type Action struct {
	Id                 uint       `gorm:"primaryKey" json:"id"`
	MenuId             string     `json:"menu_id"`
	Name               string     `json:"name"`
	ActionName         string     `json:"action_name"`
	Type               int8       `json:"type"`
	Icon               string     `json:"icon"`
	Pagesize           string     `json:"pagesize"`
	GroupButtonStatus  int8       `json:"group_button_status"`
	ListButtonStatus   int8       `json:"list_button_status"`
	ButtonColor        string     `json:"button_color"`
	Fields             Fields     `json:"fields"`
	Sortid             int64      `json:"sortid"`
	Orderby            string     `json:"orderby"`
	TreeConfig         string     `json:"tree_config"`
	Jump               string     `json:"jump"`
	ServerCreateStatus int8       `json:"server_create_status"`
	VueCreateStatus    int8       `json:"vue_create_status"`
	SearchField        Fields     `json:"search_field"`
	CacheTime          int64      `json:"cache_time"`
	ApiAuth            int8       `json:"api_auth"`
	ImgAuth            int8       `json:"img_auth"`
	SmsAuth            int8       `json:"sms_auth"`
	ServiceStatus      int8       `json:"service_status"`
	ListFilter         ListFilter `json:"list_filter"`
	TabConfig          TabConfig  `json:"tab_config"`
	Sql                string     `json:"sql"`
	DialogSize         string     `json:"dialog_size"`
	StatusVal          string     `json:"status_val"`
	Validate           string     `json:"validate"`
	SelectType         int8       `json:"select_type"`
	TableHeight        string     `json:"table_height"`
	LeftTreeSql        string     `json:"left_tree_sql"`
	WithJoin           WithJoin   `json:"with_join"`
	OtherConfig        Other      `json:"other_config"`
	DialogType         string     `json:"dialog_type"`
	LogStatus          int8       `json:"log_status"`
	Version            int        `json:"version"`
}

func (Action) TableName() string {
	return "cd_action"
}
